package com.diycode.controller;

import com.diycode.entity.Tickets;
import com.diycode.service.TicketsService;
import com.diycode.service.TrainsService;
import com.diycode.utils.MapControl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author diycode
 */
@Controller
@RequestMapping("/tickets")
public class TicketsController {

    private static final String LIST = "tickets/list";
    private static final String ADD = "tickets/add";

    @Resource
    private TicketsService ticketsService;
    @Resource
    private TrainsService trainsService;

    //跳转列表页面
    @GetMapping("/list")
    public String list() {
        return LIST;
    }

    //查询
    @PostMapping("/query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Tickets tickets) {
        PageInfo<Tickets> pageInfo = ticketsService.query(tickets);
        return MapControl.getInstance().success().page(pageInfo.getList(), (int) pageInfo.getTotal()).getMap();
    }

    //根据id查询，跳转修改页面
    @GetMapping(value = {"/detail", "/detail/{id}"})
    public String detail(@PathVariable(value = "id", required = false) Integer id, ModelMap modelMap) {
        if (id != null) {
            //查询出要修改的信息
            Tickets tickets = ticketsService.detail(id);
            //将查询出来的数据存储到request域，实现表单回显
            modelMap.addAttribute("tickets", tickets);
        }
        modelMap.addAttribute("trains", trainsService.list());
        return ADD;
    }

    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Map<String, Object> saveOrUpdate(@RequestBody Tickets tickets) {
        Boolean result = ticketsService.saveOrUpdate(tickets);
        if (result) {
            return MapControl.getInstance().success().getMap();
        }
        return MapControl.getInstance().error().getMap();
    }

    //根据id删除
    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        int result = ticketsService.delete(id);
        if (result <= 0) {
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    //批量删除
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String ids) {
        int result = ticketsService.delete(ids);
        if (result <= 0) {
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @GetMapping("/findByTrainID")
    @ResponseBody
    public Map<String, Object> findByTrainID(Integer trainID) {
        return MapControl.getInstance().success().add("tickets", ticketsService.findByTrainID(trainID)).getMap();
    }

}
